﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using Fatec.ControleEstoque.Attributes.Templates;

namespace Fatec.ControleEstoque.Interface
{
    public partial class ParentDetails_EditField : FieldTemplateUserControl
    {
        protected MetaTable parentTable;
        protected MetaTable childTable;

        public Boolean EnableDelete { get; set; }
        //public Boolean EnableInsert { get; set; }
        public Boolean EnableUpdate { get; set; }

        public String[] DisplayColumns { get; set; }

        public ParentDetails_EditField()
        {
            // set default values
            EnableDelete = true;
            EnableUpdate = true;
            //EnableInsert = false;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            var attribute = Column.Attributes.OfType<ShowColumnsAttribute>().SingleOrDefault();

            if (attribute != null)
            {
                if (!attribute.EnableDelete)
                    EnableDelete = false;
                if (!attribute.EnableUpdate)
                    EnableUpdate = false;
                //if (!attribute.EnableInsert)
                //    EnableInsert = false;
                if (attribute.DisplayColumns.Length > 0)
                    DisplayColumns = attribute.DisplayColumns;
            }

            var metaForeignKeyColumn = Column as MetaForeignKeyColumn;

            if (metaForeignKeyColumn != null)
            {
                childTable = metaForeignKeyColumn.Table;

                // setup data source
                DetailsDataSource.ContextTypeName = metaForeignKeyColumn.ParentTable.DataContextType.FullName;
                DetailsDataSource.TableName = metaForeignKeyColumn.ParentTable.Name;

                // enable update, delete and insert
                DetailsDataSource.EnableDelete = EnableDelete;
                DetailsDataSource.EnableInsert = false; // EnableInsert;
                DetailsDataSource.EnableUpdate = EnableUpdate;
                DetailsView1.AutoGenerateDeleteButton = EnableDelete;
                DetailsView1.AutoGenerateInsertButton = false; // EnableInsert;
                DetailsView1.AutoGenerateEditButton = EnableUpdate;

                // get an instance of the MetaTable
                parentTable = DetailsDataSource.GetTable();

                // Generate the columns as we can't rely on 
                // DynamicDataManager to do it for us.
                DetailsView1.RowsGenerator = new FieldTemplateRowGenerator(parentTable, DisplayColumns);

                // setup the GridView's DataKeys
                String[] keys = new String[metaForeignKeyColumn.ParentTable.PrimaryKeyColumns.Count];
                int i = 0;
                foreach (var keyColumn in metaForeignKeyColumn.ParentTable.PrimaryKeyColumns)
                {
                    keys[i] = keyColumn.Name;
                    i++;
                }
                DetailsView1.DataKeyNames = keys;

                // enable AutoGenerateWhereClause so that the WHERE 
                // clause is generated from the parameters collection
                DetailsDataSource.AutoGenerateWhereClause = true;

                // doing the work of this above because we can't
                // set the DynamicDataManager table or where values
                //DynamicDataManager1.RegisterControl(DetailsView1, false);
            }
            else
            {
                // throw an error if set on column other than MetaChildrenColumns
                throw new InvalidOperationException("The GridView FieldTemplate can only be used with MetaChildrenColumns");
            }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            var metaForeignKeyColumn = Column as MetaForeignKeyColumn;

            if (metaForeignKeyColumn != null)
            {
                // setup the where clause 
                // support composite foreign keys
                foreach (String fkName in metaForeignKeyColumn.ForeignKeyNames)
                {
                    // get the current fk column
                    var fkColumn = childTable.GetColumn(fkName);

                    // setup parameter
                    var param = new Parameter();
                    param.Name = fkColumn.Name;
                    param.Type = fkColumn.TypeCode;

                    // get the value for this FK column
                    param.DefaultValue = GetColumnValue(fkColumn).ToString();

                    // add the where clause
                    DetailsDataSource.WhereParameters.Add(param);
                }
            }

            base.OnDataBinding(e);
        }
    }
}